﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace netWebFroms
{
    public partial class _Default : Page
    {
        public static DataTable Data = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            //回发
            if(!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            var sql = "select * from Articles";
            var dt = DbHelper.GetData(sql);

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        //编辑
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            FillData();
        }
        //更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var Id = GridView(e.RowIndex, 0);
            var Title = GridView(e.RowIndex, 1);
            var Content = GridView(e.RowIndex, 2);
            var Author = GridView(e.RowIndex, 3);
            var Remarks = GridView(e.RowIndex, 4);

            var GET = int.TryParse(Id, out int id);

            if(GET)
            {
                //更新
                var sql = string.Format("update Articles set Title ='{0}',Content ='{1}',Author ='{2}',Remarks ='{3}'where Id ='{4}'", Title, Content, Author,Remarks , Id);
                DbHelper.AddOrUpdateOrDate(sql);
            }
            else
            {
                //添加
                var sql = string.Format("insert into Articles(Title,Content,Author,Remarks)values('{0}','{1}','{2}','{3}')", Title, Content, Author, Remarks);
                DbHelper.AddOrUpdateOrDate(sql);
            }
            GridView1.EditIndex = -1;
            FillData();
        }

        private string GridView(int rowIndex, int v)
        {
            var cell = GridView1.Rows[rowIndex].Cells[v];
            string values = string.Empty;

            if (cell.Controls.Count > 0)
            {
                var t = cell.Controls[0];
                var get = (TextBox)t;
                values = get.Text;
            }
            else
            {
                values = cell.Text;
            }
            return values;
        }

        //删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var Id = GridView(e.RowIndex, 0);

            var sql = string.Format("delete from Articles where Id ='{0}'",Id);
            DbHelper.AddOrUpdateOrDate(sql);

            GridView1.EditIndex = -1;
            FillData();
        }
        //取消
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            FillData();
        }
        //新增
        protected void Added_Click(object sender, EventArgs e)
        {
            var sql = "select * from Articles";
            Data = DbHelper.GetData(sql);
            var dt = Data.Copy();
            dt.Rows.Add(dt.NewRow());
            GridView1.DataSource = dt;
            GridView1.EditIndex = dt.Rows.Count - 1;
            GridView1.DataBind();
        }
    }
}